<?php
namespace Home\Controller;
use \Think\Controller;

class StudentController extends Controller{

	public function index(){
		$model = D('student');

		//连贯操作 where
		$data  = $model->where('id > 10')->select();
		$data = $model->table('test')->select();
		$data = $model->alias('s')->select();
		$data = $model->field('id,age')->select();
		$data = $model->limit('0,10')->select();
		$data = $model->page('2,10')->select();
		$data = $model->group('sex')->select();
		$data = $model->field('sex,count(*) as total')->group('sex')->having('total > 30')->select();

		//查询方式
		$where = "id > 10";
		$data = $model->where($where)->select();

		$where = array();
		$where['sex'] = '1';
		$where['age'] = '18';
		$where['_logic'] = 'or';
		$data = $model->where($where)->select();

		$obj = new \StdClass();
		$obj->name = 'yanyan';
		$obj->grade = 's45';
		$obj->_logic = 'or';
		$data = $model->where($obj)->select();

		$where = array();
		$where['name'] = array('like','%yan%');
		$where['age'] = array('lt','18');
		$data = $model->where($where)->select();

		$where = array();
		//$where["id|sex"] = '1';
		//$where["id&sex&grade"] = '1';
		$where["id|sex"] = array('10','2','_multi'=>true);
		$data = $model->where($where)->select();

		$where = array();
		$where['id'] = array(array('gt','10'),array('lt','100'),'or');

		$data = $model->where($where)->select();
		
		//where age > 18 or (name like '%yan%' and sex = '2'); 

		$where = array();
		$where['name'] = array('like','%yan%');
		$where['sex'] = '2';
		$map = array();
		$map['age'] = array('gt','18');
		$map['_complex'] = $where;
		$map['_logic'] = 'or';

		//$sql = $model->where($map)->select(false);
		$subSql = $model->table('test')->field('id')->buildSql();

		$data  = $model->table($subSql.' t')->select();
		echo "<hr>";
		/*
		$data = $model->count();
		$data = $model->max('id');
		$data = $model->min('id');
		$data = $model->avg('id');
		$data = $model->sum('id');

		$data = $model->field('id')->getByName('yanyan');
		$data = $model->getFieldByName('yanyan','id');
		*/


		echo "LAST SQL：".$model->getLastSql();
		dump($data);
	}
}